package test;

import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 
public class FormServlet extends HttpServlet {
 
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		getServletContext().getRequestDispatcher("/WEB-INF/pages/form.jsp").forward(req, resp);
	}
 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userName = request.getParameter("username");
		String password = request.getParameter("password");
		String bio = request.getParameter("bio");
		String country = request.getParameter("country");
		String subscribeForNewsletter = request.getParameter("newsletter");
		String receiveMail = request.getParameter("receivemail");
		String reveiveNotifications = request.getParameter("receivenotification");
 
		response.setContentType("text/html");
 
		PrintWriter writer = response.getWriter();		
		writer.write("<h2> You have entered following values </h2>");
	
		writer.write("<br/>");
		writer.write("<table>");
		startTableRow(writer);
		addTableColumn(writer, "username");
		addTableColumn(writer, userName);
		closeTableRow(writer);
 
		startTableRow(writer);
		addTableColumn(writer, "password");
		addTableColumn(writer, password);
		closeTableRow(writer);
 
		startTableRow(writer);
		addTableColumn(writer, "Bio");
		addTableColumn(writer, bio);
		closeTableRow(writer);
 
		startTableRow(writer);
		addTableColumn(writer, "Country");
		addTableColumn(writer, country);
		closeTableRow(writer);
 
		startTableRow(writer);
		addTableColumn(writer, "Subscribe for news letter");
		addTableColumn(writer, subscribeForNewsletter);
		closeTableRow(writer);
 
		startTableRow(writer);
		addTableColumn(writer, "Receive mail from members");
		addTableColumn(writer, receiveMail);
		closeTableRow(writer);
 
		startTableRow(writer);
		addTableColumn(writer, "Receive notification");
		addTableColumn(writer, reveiveNotifications);
		closeTableRow(writer);
 
		writer.write("</table>");
 
		writer.close();
 
	}
 
	private static void startTableRow(PrintWriter writer) {
	writer.write("<tr>");	
	}
 
	private static void closeTableRow(PrintWriter writer) {
		writer.write("</tr>");	
	}
 
	private static void addTableColumn(PrintWriter writer, String value) {
		writer.write("<td> "+ value + "</td>");	
	}
 
 
}